import Vue from 'vue'
import Vuex from 'vuex'
import SystemModule from './SystemModule'

Vue.use(Vuex)

const toTree = (item)=>{
	var obj = {};
	var length = item.length;
	for(let i=0;i<length;i++){
		var permission = item[i];
		if(!permission.parentid){
			permission.children = [];//为该权限创建一个空的子权限列表 
			obj[permission._id]=permission;//以改圈先的id为属性名,该权限作为值,存储到obj中,为了子权限可以通过该属性名来找到父级权限
		}
	}
for(let i=0;i<length;i++){
      var permission = item[i];
      for(var key in obj){
         if(permission.parentid == key){
            obj[key].children.push(permission);
         }
      }
   }
   var arr = [];
   for(let key in obj){
      arr.push(obj[key])
   }
   return arr;
}

export default new Vuex.Store({
	modules: {SystemModule},
  	state: {
		accountPermission:[],
		accountRoles:[]
  	},
	getters: {
  	accountPermission(){
		return toTree(JSON.parse(localStorage.getItem("accountInfo")).permissions);					
  	},
  	accountRoles(){
		return JSON.parse(localStorage.getItem("accountInfo")).roles;
  	}
 	},
  	mutations: {

 	},
  	actions: {

  	}
})
